package com.hksj.app.service;


import cn.hutool.http.HttpStatus;
import com.alibaba.fastjson.JSONObject;
import com.hksj.app.websocket.MessageType;
import com.hksj.app.websocket.WebsocketConnectionHolder;
import com.hksj.app.websocket.WebsocketResponse;
import com.hksj.em.dto.create.SurgicalDeviceControlDTO;
import org.springframework.stereotype.Service;

@Service
public class OrEquipmentService {

    public Boolean equipmentControl(SurgicalDeviceControlDTO dto) {
        // 枚举:通过设备functionCode判断执行动作
        switch (dto.getFunctionCode()) {
            case "LIGHT_POWER_ON1":
            case "LIGHT_POWER_OFF1":
            case "LIGHT_POWER_ON2":
            case "LIGHT_POWER_OFF2":
            case "AC_POWER_ON":
            case "AC_POWER_OFF":
                // 通过WebSocket转发控制指令给情报面板
                forwardControlCommandToDesktop(dto);
                break;
            default:
                break;
        }
        return true; // 假设总是成功
    }

    private void forwardControlCommandToDesktop(SurgicalDeviceControlDTO dto) {
        // 创建WebSocket响应消息
        WebsocketResponse response = new WebsocketResponse();
        response.setType(MessageType.EQ_CONTROL.value()); // 自定义消息类型
        response.setCode(HttpStatus.HTTP_OK); // 200表示成功

        JSONObject controlData = new JSONObject();
        controlData.put("functionCode", dto.getFunctionCode());
        response.setData(controlData);
        // 发送给桌面应用端（假设桌面端使用固定code"desktop_app"连接）
        WebsocketConnectionHolder.sendMessage(dto.getDeviceId(), response);
    }
}
